package de.tu_darmstadt.gdi1.battleship.view;

import java.awt.Color;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import de.tu_darmstadt.gdi1.battleship.exceptions.InternalFailureException;
import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidOperationException;
import de.tu_darmstadt.gdi1.battleship.exceptions.ParameterOutOfRangeException;
import de.tu_darmstadt.gdi1.battleship.ui.GamePanel;

public class StudentPanel extends GamePanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4339872899724848293L;
	private StudentWindow parentWindow;
	private Skin skin;
	
	public StudentPanel(StudentWindow theParentWindow) {
		super(theParentWindow);
		parentWindow = theParentWindow;
		// setAutosize(true);
	}

	/**
	 * Creates the contents of a game panel , the maps with its skin
	 */
	protected void setGamePanelContents() {

		// prepare images
		if (skin == null) loadSkin();

		// check if map is loaded
		if (parentWindow.getBsg() != null
				&& parentWindow.getBsg().getHumanMap() != null
				&& parentWindow.getBsg().getCpuMap() != null) {

			// get characters of the map. Of the CpuMap only parts may be shown.
			char[][] humanView = parentWindow.getBsg().getHumanMap()
					.getCharView();
			char[][] cpuView = parentWindow.getBsg().getCpuMap().getEnemyView();

			// set the correct images

			try {
				for (int r = 0; r < 10; r++) {
					for (int c = 0; c < 10; c++) {
						placeEntity(((Character) humanView[c][r]).toString());
					}
					placeEntity("|");
					for (int c = 0; c < 10; c++) {
						placeEntity(((Character) cpuView[c][r]).toString());
					}

				}
			} catch (ParameterOutOfRangeException e) {
				System.err.println("Map contains invalid symbols.");
			} catch (NullPointerException e) {
				System.err.println("Error in map, maybe no map loaded?");
				e.printStackTrace();
			}
		}

		// try {
		// notifyLevelLoaded(20, 10);
		// } catch (ParameterOutOfRangeException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (InternalFailureException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}
/**
 * @see
 * de.tu_darmstadt.gdi1.battleship.view.StudentPanel#loadSkin(Skin)
 */
	public void loadSkin() {
		loadSkin(new Skin());
	}
/**
 * loads a skin and implements it for that map
 * @param skin
 */
	public void loadSkin(Skin skin) {

		// Try to load the images
		for (String str : skin.getImages().keySet()) {
			try {
				if (isImageRegistered(str))
				 unregisterImage(str);
				 registerImage(str, skin.getImages().get(str));
			} catch (ParameterOutOfRangeException e) {
				e.printStackTrace();
			} catch (InvalidOperationException e) {
				e.printStackTrace();
			} catch (InternalFailureException e) {
				e.printStackTrace();
			}
		}
		
		this.skin = skin;
	}

	@Override
	protected void entityClicked(int positionX, int positionY) {

		parentWindow.getMainCtrl().userClicked(positionX, positionY);

	}
	
	
	//size of the ship to place
	//0: no ship
	//1: can't place
	//2-5: size if vertical
	//12-15: size if horizontal
	private int placeMode = 0;
	
	@Override
	public void mouseEntered(MouseEvent evt) {
		JButton src = (JButton) evt.getSource();
		int i = entities.indexOf(src);
		parentWindow.getCtrl().setPPMode(i % 21, i / 21);
		switch (placeMode){
		case 1:
			src.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
			break;
		case 2:
		case 3:
		case 4:
		case 5:
			for (int j = 0; j<placeMode; j++)
			entities.get(i+j*21).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GREEN));
			break;
		case 12:
		case 13:
		case 14:
		case 15:
			for (int j = 0; j<placeMode-10; j++)
			entities.get(i+j).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.GREEN));
			break;
	    default:
		}
	}
	
	@Override
	public void mouseExited(MouseEvent evt) {
		JButton src = (JButton) evt.getSource();
		int i;
		switch (placeMode){
		case 1:
			src.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
			break;
		case 2:
		case 3:
		case 4:
		case 5:
			i = entities.indexOf(src);
			for (int j = 0; j<placeMode; j++)
			entities.get(i+j*21).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
			break;
		case 12:
		case 13:
		case 14:
		case 15:
			i = entities.indexOf(src);
			for (int j = 0; j<placeMode-10; j++)
			entities.get(i+j).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
			break;
	    default:
		}
	}


	@Override
	protected void panelResized() {
		parentWindow.pack();
	}

	public void setplaceMode(int i){
		placeMode = i;
	}
}
